/**
 * Created by xuehuan on 2016/4/7.
 */
import React from 'react';
import IndexCarousel from './carousel';
import Menu from './searchMenu';
import OrgList from './orgList';
import Loading from './loading';
import Fetch from './fetch';
class Index extends React.Component{
    constructor(){
        super();
        this.requestData = this.requestData.bind(this);
        this.state = {
                data: {
                    "school": [],
                    "type": [],
                    "team": []
                },
                reqData: {
                    "page": 1,
                    "status": 0,
                    'type': '',
                    "school": ''
                }
        };

        var _this = this;

        this.requestData();

        window.addEventListener('scroll',function(){
            _this.loadNext();
        })
    }
    requestData(p) {
        var _this = this;
        var page = p ? p : this.state.page;
        Fetch('http://121.42.195.176/EServise/index.php/Home/Team/getTeamList',
              'POST',
              _this.state.reqData,
              _this.callback,_this)
    }
    callback(data) {
        var load = document.getElementById('loading');
        if(data.team.length){
            var newData = this.state.data.team.concat(data.team);
                this.state.data.team = newData;
                this.state.reqData.page ++;
                this.setState({'data':this.state.data,'reqData': this.state.reqData})
                load.className='hide';
        }else{
            load.innerHTML('没有更多了~_~');
        }
    }
    loadNext(){
        var doc = document.documentElement;
        var load = document.getElementById('loading');
        if(doc.scrollHeight - doc.scrollTop <= 480 ){
            document.getElementById('loading').className='';
            this.requestData(this.state.page);
        }
    }
    render() {
        return (
            <div>
                <IndexCarousel/>
                <Menu />
                <OrgList data={this.state.data}/>
                <Loading />
            </div>

        );
    }
}
export default Index;